﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D rb;

    public Collider2D collider;
    
    public Animator anim;

    public float speed = 10f;

    public float jumpforce = 10f;

    public LayerMask ground;

    [SerializeField]
    public int Cherry = 0;

    [SerializeField] 
    private Text cherryNum;

    // 
    private bool isHurt;

    private static readonly int Falling = Animator.StringToHash("falling");
    private static readonly int Idle = Animator.StringToHash("idle");
    private static readonly int Jumping = Animator.StringToHash("jumping");
    private static readonly int Running = Animator.StringToHash("running");

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    // 固定更新
    void FixedUpdate()
    {
        if (!isHurt)
        {
            Movement();
        }
        SwitchAnim();
    }
    

    void Movement()
    {
       float horizontalMove =  Input.GetAxis("Horizontal");
       float facedirection = Input.GetAxisRaw("Horizontal");
       if (horizontalMove != 0)
       {
           rb.velocity = new Vector2(horizontalMove * speed * Time.deltaTime, rb.velocity.y);
           anim.SetFloat(Running,Mathf.Abs(facedirection));
       }

       if (facedirection != 0)
       {
           transform.localScale = new Vector3(facedirection, 1, 1);
       }

       if (Input.GetButtonDown("Jump") && collider.IsTouchingLayers(ground))
       {
           rb.velocity = new Vector2(rb.velocity.x, jumpforce * Time.deltaTime);
           anim.SetBool(Jumping,true);
       }
       
    }

    void SwitchAnim()
    {
        anim.SetBool(Idle,false);
        if (anim.GetBool(Jumping))
        {
            if (rb.velocity.y < 0)
            { 
                anim.SetBool(Jumping,false);
                anim.SetBool(Falling,true);
            }
        }
        else if (isHurt)
        {
            anim.SetBool("hurt", true);
            anim.SetFloat(Running, 0);
            if (Mathf.Abs(rb.velocity.x) < 0.1)
            {
                anim.SetBool("hurt", false);
                anim.SetBool(Idle, true);
                isHurt = false;
            }
        }
        else if (collider.IsTouchingLayers(ground))
        {
            anim.SetBool(Falling,false);
            anim.SetBool(Idle,true);
        }
    }


    // 收集
    private void OnTriggerEnter2D(Collider2D collision)
    {
    
        if (collision.CompareTag("Collection"))
        {
            Destroy(collision.gameObject);
            Cherry += 1;
            cherryNum.text = Cherry.ToString();
        }
    }
    
    // 消灭敌人
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {
            if (anim.GetBool(Falling))
            {
                Destroy(other.gameObject);
                rb.velocity = new Vector2(rb.velocity.x, jumpforce * 0.5F * Time.deltaTime);
                anim.SetBool(Jumping,true);
            }
            else if(transform.position.x < other.gameObject.transform.position.x)
            {                
                rb.velocity = new Vector2(-10, rb.velocity.y);
                isHurt = true;
            }
            else if (transform.position.x > other.gameObject.transform.position.x)
            {
                rb.velocity = new Vector2(10, rb.velocity.y);
                isHurt = true;
            }
        }
    }
}
